<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="600"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
	项目信息
      <a-form-item label="项目标题" field="project_title">
        <a-input v-model="formData.project_title" placeholder="请输入项目标题" />
      </a-form-item>
      <a-form-item label="租赁方式" field="ental_structure">
        <a-input v-model="formData.ental_structure" placeholder="请输入租赁方式" />
      </a-form-item>
	  <a-form-item label="起租期" field="lease_term">
	    <a-input v-model="formData.lease_term" placeholder="请输入起租期" />
	  </a-form-item>
<!--      <a-form-item label="建设竣工时间" field="floor">-->
<!--        <a-date-picker v-model="formData.floor" :show-time="false" mode="date" placeholder="请选择建设竣工时间" />-->
<!--      </a-form-item>-->
      <a-form-item label="楼层" field="floor">
        <a-input v-model="formData.floor" placeholder="请输入楼层" />
      </a-form-item>
	  <a-form-item label="层高" field="standard_height">
	    <a-input v-model="formData.standard_height" placeholder="请输入层高" />
	  </a-form-item>
      <a-form-item label="可办环评" field="environmental_impact">
        <a-input v-model="formData.environmental_impact" placeholder="请输入可办环评" />
      </a-form-item>
	  <a-form-item label="承重" field="standard_load_bearing">
	    <a-input v-model="formData.standard_load_bearing" placeholder="请输入承重" />
	  </a-form-item>

      <a-form-item label="厂房新旧" field="old_and_new">
        <a-input v-model="formData.old_and_new" placeholder="请输入厂房新旧" />
      </a-form-item>

      <a-form-item label="厂房结构" field="structure">
        <a-input v-model="formData.structure" placeholder="请输入厂房结构" />
      </a-form-item>
<!--	  <a-form-item label="空调费" field="air_conditioning_fee">-->
<!--	    <a-input v-model="formData.air_conditioning_fee" placeholder="请输入空调费" />-->
<!--	  </a-form-item>-->
	  配套设施
	  <a-form-item label="水" field="water">
	   <sa-switch v-model="formData.water" checkedText="是"  uncheckedText="否"/>
	  </a-form-item>
	  <a-form-item label="电" field="electricity">
      <sa-switch v-model="formData.electricity" checkedText="是"  uncheckedText="否"/>
	  </a-form-item>
	  <a-form-item label="燃气" field="gas">

      <sa-switch v-model="formData.gas" checkedText="是"  uncheckedText="否"/>
	  </a-form-item>
	  <a-form-item label="电压气" field="piezoelectric_gas">

      <sa-switch v-model="formData.piezoelectric_gas" checkedText="是"  uncheckedText="否"/>
	  </a-form-item>
	  <a-form-item label="货梯" field="lift">

      <sa-switch v-model="formData.lift" checkedText="是"  uncheckedText="否"/>
	  </a-form-item>
	  <a-form-item label="客梯" field="elevator">
      <sa-switch v-model="formData.elevator" checkedText="是"  uncheckedText="否"/>
	  </a-form-item>
      <a-form-item label="配套办公" field="facilities">
        <sa-switch v-model="formData.facilities" checkedText="是"  uncheckedText="否"/>
      </a-form-item>
      <a-form-item label="宿舍" field="dormitory">
        <sa-switch v-model="formData.dormitory" checkedText="是"  uncheckedText="否"/>
      </a-form-item>
      <a-form-item label="餐厅" field="restaurant">
        <sa-switch v-model="formData.restaurant" checkedText="是"  uncheckedText="否"/>
      </a-form-item>

      <a-form-item label="停车场" field="parking_lot">
        <sa-switch v-model="formData.parking_lot" checkedText="是"  uncheckedText="否"/>
      </a-form-item>


    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import api from '../api/project'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')

let title = computed(() => {
  return '项目列表' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  project_id: null,
  lang_code: '',
  project_title: '',
  floor_area: '',
  completion_time: '',
  building_number: null,
  property_management_fee: '',
  plot_ratio:'',
  air_conditioning_fee:'',

  standard_height:'',
  structure:'',
  old_and_new:'',
  environmental_impact:'',
  lease_term:'',
  standard_load_bearing:'',
  ental_structure:'',
  floor:'',

  water:0,
  electricity:0,
  gas:0,
  piezoelectric_gas:0,
  lift:0,
  elevator:0,
  facilities:0,
  dormitory:0,
  restaurant:0,
  parking_lot:0,
})

// 验证规则
const rules = {
  lang_code: [{ required: true, message: '语言代码，如zh-CN、en-US等必需填写' }],
  project_title: [{ required: true, message: '项目标题必需填写' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.project_id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.project_id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
